import {
    Bseurl
} from "../baseurl/baseurl"
import
encrypt
from "../utils/crypto-util.js"
//调用随机数函数的随机数
function getUuid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = (Math.random() * 16) | 0,
            v = c == 'x' ? r : (r & 0x3) | 0x8;
        return v.toString(16);
    });
}
export const request = (url, method, data, contype, deviceId) => {
    // 获取当前时间戳
    let datatime = new Date().getTime()
    console.log(data);
    let Randomnum = getUuid()
    let aecdata
    let md5
    if (!data) {
        console.log(data, '不传输数据');
        md5 = encrypt.md5(`sign:data=&nonceStr=${Randomnum}&timestamp=${datatime}`)
    } else {
        console.log(data, '传输数据');
        // aec加密数据
        aecdata = encrypt.aesEncrypty(data)
        // 获取MD5加密数据
        md5 = encrypt.md5(`sign:data=${aecdata}&nonceStr=${Randomnum}&timestamp=${datatime}`)
    }
    return new Promise((resolve, reject) => {
        wx.request({
            url: Bseurl + url,
            method: method,
            data: aecdata,
            header: {
                "h_uid": wx.getStorageSync('userid') ? wx.getStorageSync('userid') : '',
                "h_timestamp": datatime,
                "h_nonceStr": Randomnum,
                "h_sign": md5,
                "h_token": wx.getStorageSync('token') ? wx.getStorageSync('token') : '',
                "h_deviceId": deviceId ? deviceId : '',
                'content-type': 'application/json'
            },
            success(res) {
                console.log(res);
                // console.log(encrypt.aesDecrypt(res.data));
                // 检测token
                // if (JSON.parse(encrypt.aesDecrypt(res.data)).token) {
                //     let token = JSON.parse(encrypt.aesDecrypt(res.data)).token
                //     let userid = JSON.parse(encrypt.aesDecrypt(res.data)).data.id
                //     wx.setStorageSync("token", token)
                //     wx.setStorageSync("userid", userid)
                // }
                let data = encrypt.aesDecrypt(res.data)
                console.log(JSON.stringify(data), data, '返回值');
                resolve(JSON.parse(encrypt.aesDecrypt(res.data)))
                //  resolve(data)
            },
            fail(err) {
                reject(err)
            }
        })
    })
}